using UnityEngine;
using System;

namespace hatbox_hack_test
{
    public static class Utils
    {
        public static void PrintException(Exception ex)
        {
            hatbox_hack.GameObjects.hatboxHack.log("当前时间：" + DateTime.Now.ToString());
            hatbox_hack.GameObjects.hatboxHack.log("异常信息：" + ex.Message);
            hatbox_hack.GameObjects.hatboxHack.log("异常对象：" + ex.Source);
            hatbox_hack.GameObjects.hatboxHack.log("调用堆栈：\n" + ex.StackTrace.Trim());
            hatbox_hack.GameObjects.hatboxHack.log("触发方法：" + ex.TargetSite);
        }
    }

    public static class DisplayAndHighlight<T>
        where T : MonoBehaviour
    {
        public static void DisplayAndHighlightObject(T monoBehaviour)
        {
            // 获取屏幕中心点的位置
            Vector3 screenCenter = new Vector3(Screen.width / 2f, Screen.height / 2f, 0f);

            // 获取MonoBehaviour的大小
            Bounds bounds = GetBounds(monoBehaviour);

            // 计算MonoBehaviour的屏幕位置
            Vector3 position = screenCenter - bounds.center;

            // 设置MonoBehaviour的位置为屏幕中心
            monoBehaviour.transform.position = position;
        }

        private static Bounds GetBounds(T monoBehaviour)
        {
            // 获取MonoBehaviour的所有渲染器
            Renderer[] renderers = monoBehaviour.GetComponentsInChildren<Renderer>();

            // 如果没有渲染器，则使用MonoBehaviour的Collider来计算边界
            if (renderers.Length == 0)
            {
                Collider collider = monoBehaviour.GetComponent<Collider>();
                if (collider != null)
                {
                    return collider.bounds;
                }
            }
            else
            {
                // 计算所有渲染器的合并边界
                Bounds bounds = renderers[0].bounds;
                for (int i = 1; i < renderers.Length; i++)
                {
                    bounds.Encapsulate(renderers[i].bounds);
                }
                return bounds;
            }

            // 如果没有渲染器和Collider，则返回一个默认的边界
            return new Bounds(Vector3.zero, Vector3.zero);
        }
    }
}
